(function(global, document, _fns, $, console) {
  'use strict';

  /*
  
      @name 自动嵌套命名空间
  
      @desc
  
      @author
  
      @access protected
  
      @function _extend
  
      @param {Object} ns 顶级命名空间
  
      @param {string} [nsString = tlns.ns1.ns2.ns3] 基于顶级命名空间要嵌套的命名空间
  
      @returns {Object} 返回嵌套好的对象。
  
      @example
          // " tlns.sample = $.noop; " === " tlns.ns1.ns2.ns3.sample = $.noop; "
          var tlns = _extend(tlns, "tlns.ns1.ns2.ns3");
  
      @todo 将这个函数放入公共函数库中。
   */
  var _extend, exc;
  _extend = function(ns, nsString) {
    var i, parent, parts, pl;
    parts = nsString.split(".");
    parent = ns;
    pl = parts.length;
    i = 0;
    while (i < pl) {
      if (typeof parent[parts[i]] === "undefined") {
        parent[parts[i]] = {};
      }
      parent = parent[parts[i]];
      i++;
    }
    return parent;
  };
  exc = _extend(_fns, "_fns.excute");

  /*
      @desc
  
      @author
  
      @access protected
  
      @function exc._fast_click
   */
  exc.sidebar = function() {
    global.lsBar();
  };
  /*切换订单按钮方法*/
  exc.changeOrder = function(){
	var cutBtn = $(".cut-btn"),
	    mallOrder = $(".mall-order-list"),
	    actOrder = $(".act-order-list");
		cutBtn.on("click",function(){
			cutBtn.removeClass("cur");
			var thisElm = $(this);
			thisElm.addClass("cur");
			if(thisElm.attr("btntype") == "mall"){
				mallOrder.css("display","block");
				actOrder.css("display","none");
			}else if(thisElm.attr("btntype") == "activity"){
				actOrder.css("display","block");
				mallOrder.css("display","none");
			}
		});
  };
  /*
      @desc
  
      @author
  
      @access protected
  
      @function exc._cancelOrder
   */
  exc._cancelOrder = function() {
    var asd, btnCancelOrder, cbMdl, fnCancelOrder, mdl;
    btnCancelOrder = $('.cancel-order');
    mdl = $('#mdlCancelOrder');
    asd = mdl.html();
    mdl.remove();
    
    fnCancelOrder = function(e) {
    	
      var btnSubmit, fnCloseModal, frm, cancelOrderId, resonCode, payStatus,isNineTyTimeOut;
      
      e.stopPropagation();
      e.preventDefault();
      
      var that = $(this);
      
      cancelOrderId = that.data('orderid');
      payStatus = that.data('paystatus');
      isNineTyTimeOut = that.data('isninetytimeout');
      ECar.easyDialog.open({
        container: {
          header: '取消定单？',
          content: asd
        }
      });
      
      frm = $('#frmResnCancelOrder');
      btnSubmit = frm.find('.btn-submit');
      
      var closestTd = that.closest('td');
      
      var prevTd = closestTd.prev('td');
      fnCloseModal = function(e) {
    	
		e.stopPropagation();
		e.preventDefault();
		
		resonCode = frm.find('input[type="radio"]:checked').val();
		resonCode = encodeURI(encodeURI(resonCode));
		
        if (frm.find('input[type="radio"]:checked').length > 0) {
        	if(isNineTyTimeOut == "1"){
        		$(".easyDialog_title").html("<a href='javascript:void(0)' title='关闭窗口' class='close_btn' id='closeBtn'>×</a>信息核对");
        		$("#closeBtn").on("click",ECar.easyDialog.close);
        		//三级联动
				$.ajax({
					url:"/common/getLinkageData.htm",
					type: "GET",
					dataType: "json",
					success: function(data){
						ECar.seriesSelect($(".popup-input-box","#easyDialogWrapper"),{
							/*url:" http://car.chexiang.com/commonAjax/getMultiMenu.htm",*/
							data:data,
							nodata:null,
							required:false,
							selects:["province","city-code"],
							complete:function(){
								
							}
						});
					}
				});
        		//弹窗
        		var name,account,province,subBank,subBankName;
        		$(".frmResnCancelOrder>div").css("display","none");
        		//$(".frmResnCancelOrder").parent().find("i").css("display","none");
        		$("#easyDialog_text").css("padding","0");
        		$("#frmResnCancelOrder i").remove();
        		$(".consult").css("display","block");
        		$("#frmResnCancelOrder").css("height","495");
        		$(".input-btn").on('click',function(){
        			$(".popup-input-box .input-error").css("display","none");
        			name = $(".user-name").val();
        			account = $(".bank-account").val();
        			province = $(".province option:selected").val();
        			subBank = $(".city-code option:selected").val();
        			subBankName = $(".sub-bank-name").val();
        			console.log(name,account,province,subBank,subBankName);
        			if(VerFunc(name,account,province,subBank,subBankName)){
        				name = encodeURI(encodeURI(name));
            			subBankName = encodeURI(encodeURI(subBankName));
            			st(cancelOrderId,resonCode,payStatus,name,account,province,subBank,subBankName);
        			}
        		});
        		$(".popup-input-box .input-text").on("blur",function(){
        			$(".popup-input-box .input-error").css("display","none");
        			name = $(".user-name").val();
        			account = $(".bank-account").val();
        			subBankName = $(".sub-bank-name").val();
        			VerFuncBlur(name,account,subBankName,this);
        		});
        		return false;
        	}
          
	      
	      st(cancelOrderId,resonCode,payStatus);
	      
	      ECar.easyDialog.close();
	      
//          frm.submit();
        }
        
      };
      
      btnSubmit.on('click', fnCloseModal);
    };
    btnCancelOrder.on('click', fnCancelOrder);
  };
  
//提交验证
  var VerFunc = function(name,account,province,subBank,subBankName){
	  var ver = true;
	  var nameReg = /((^[\u4e00-\u9fa5]$)|(^([\u4e00-\u9fa5]| )+$))|(^([a-zA-Z]| )+$)/;
		var accountReg = /^\d+$/;
		var bankNameReg = /(^[\u4e00-\u9fa5]$)|(^[\u4e00-\u9fa5]([\u4e00-\u9fa5]| )*[\u4e00-\u9fa5]$)/;
		if (!nameReg.test(name)) {
			$(".user-name").siblings(".input-error").css("display","inline-block").html("请填写真实姓名");
			ver = false;
			return false;
		}
		if(!accountReg.test(account)){
			$(".bank-account").siblings(".input-error").css("display","inline-block").html("请填写正确的账号");
			ver = false;
			return false;
		}
		if(!bankNameReg.test(subBankName)){
			$(".sub-bank-name").siblings(".input-error").css("display","inline-block").html("请填写开户行(支行)名称");
			ver = false;
			return false;
		}
		if(province == "请选择开户行所在城市" || province == 0){
			$(".province").siblings(".input-error").css("display","inline-block").html("请选择开户行所在省份");
			ver = false;
			return false;
		}
		if(subBank == "请选择开户行所在城市" || subBank == 0){
			$(".city-code").siblings(".input-error").css("display","inline-block").html("请选择开户行所在城市");
			ver = false;
			return false;
		}
		return ver;
  };
//失去焦点验证
  var VerFuncBlur = function(name,account,subBankName,elm){
	  var ver = true;
	  var nameReg = /((^[\u4e00-\u9fa5]$)|(^([\u4e00-\u9fa5]| )+$))|(^([a-zA-Z]| )+$)/;
		var accountReg = /^\d+$/;
		var bankNameReg = /(^[\u4e00-\u9fa5]$)|(^[\u4e00-\u9fa5]([\u4e00-\u9fa5]| )*[\u4e00-\u9fa5]$)/;
		if($(elm).attr("intype") == "user-name"){
			if (!nameReg.test(name)) {
				$(".user-name").siblings(".input-error").css("display","inline-block").html("请填写真实姓名");
				ver = false;
				return false;
			}
		}
		if($(elm).attr("intype") == "bank-account"){
			if(!accountReg.test(account)){
				$(".bank-account").siblings(".input-error").css("display","inline-block").html("请填写正确的账号");
				ver = false;
				return false;
			}
		}
		if($(elm).attr("intype") == "sub-bank-name"){
			if(!bankNameReg.test(subBankName)){
				$(".sub-bank-name").siblings(".input-error").css("display","inline-block").html("请填写开户行(支行)名称");
				ver = false;
				return false;
			}
		}
	return ver;
  };
//提交结果
  var st = function (cancelOrderId,resonCode,payStatus,bankUserName,bankAccount,province,subBank,subBankName) {
      var url = "/member/toCancleOrder.htm?orderId="+cancelOrderId+"&resron="+resonCode;
      if(bankUserName != null && subBankName != null && bankAccount != null){
    	  url = "/member/toCancelTimeOutOrder.htm?orderId="+cancelOrderId+"&resron="+resonCode+"&bankAccount="+bankAccount+"&userName="+bankUserName+"&provinceCode="+province+"&cityCode="+subBank+"&bankName="+subBankName;
      }
 	 var ajaxCancleOrder = $.post(url);
 	 ajaxCancleOrder.done(function(data, textStatus, jqXHR) {
 		 if(data=='true'){
 			 if(payStatus=='1'){
 				 ECar.easyDialog.open({
 					 container : {
 						 header : '提示',
 						 content : '<div style="text-align:center">申请定单取消已提交</div>'
 					 },
 					 callback: function(){window.location.reload(true);} 
 				 });
 			 }else{
 				 ECar.easyDialog.open({
 					 container : {
 						 header : '提示',
 						 content : '<div style="text-align:center">定单取消成功</div>'
 					 },
 					 callback: function(){window.location.reload(true);} 
 				 });
 			 }
 		 }else{
 			 ECar.easyDialog.open({
 				 container : {
 					 header : '提示',
 					 content : '<div style="text-align:center">定单取消失败</div>'
 				 }
 			 }); 
 		 }
      });
 	 ajaxCancleOrder.fail(function(jqXHR, textStatus, errorThrown) {
    	  ECar.easyDialog.open({
				container : {
					header : '提示',
					content : '<div style="text-align:center">定单取消失败</div>'
				}
			});
      });
   };
  var btnChkOrder = $('.action').find('.check-order');
 
  //检查定单是否支付
  var checkOrderPayStatus = function (orderId, payUrl, orderType) {
	  //活动订单
  	 var url_start;
	 if(orderType && orderType == 'promotion'){
	  	url_start = "/member/proOrderStatus/";
	 }else{
	  	url_start = "/member/orderStatus/";
	 }
  	$.get(url_start+orderId+".htm",
  	function(data) {
  		if('true'==data){
  			window.location.assign(payUrl);
  		}else{
  		  ECar.easyDialog.open({
				container : {
					header : '提示',
					content : '<div style="text-align:center">无效定单!</div>'
				}
			});
  		}
  	});
  	return true;
  }
  btnChkOrder.on('click', function (e) {
	  e.stopPropagation();
	  e.preventDefault();
	  var that;
	  that = $(this);
	  var payType = that.attr('payType');
	  /*如果payType为bigpay，则走留资上传身份证*/
	 if(payType && payType == 'bigpay'){
	 	
		  fnUploadInfo(that.data('orderid'),function(){     
		  	checkOrderPayStatus(that.data('orderid'), that.data('payurl'), that.attr('orderType'));
		  });
	  }else{
	  	checkOrderPayStatus(that.data('orderid'), that.data('payurl'), that.attr('orderType'));
	  }
  });
  
  
	$(".cancel-order-tip").click(function(){
		ECar.easyDialog.open({
			 container : {
				 header : '提示',
				 content : '<div style="text-align:center">定单无法取消，如有疑问联系4008-020-666</div>'
			 }
		});
	});
  
  /*
      @name 初始化
  
      @desc 所有在 " _fns.excute " 命名空间下定义的函数将被自动运行。
  
      @author
  
      @access protected
  
      @function _fns.init
  
      @param {Object} opts 存放定义好的可任意修改（配置）的行为选项。
   */
  _fns.init = function(opts) {
    var _it;
    for (_it in exc) {
      if (typeof exc[_it] === "function") {
        exc[_it]();
      }
    }
  };
  $(_fns.init);
})(window, document, {}, jQuery, (typeof console !== 'undefined' ? console : void 0));
